package cn.aylog.oss.controller;

import cn.aylog.common.entity.R;
import cn.aylog.oss.utils.OssUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@RestController
@RequestMapping("/file")
public class FileController {

    @Resource
    private OssUtils ossUtils;

    @PostMapping("/upload")
    public R upload(MultipartFile file) {
        String path = "";
        try {
            path = ossUtils.upload2Oss(file.getInputStream(), file.getOriginalFilename());
        } catch (IOException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        }
        return R.ok().data("path", path);
    }
}
